#include "../pch.h"

namespace BFX
{

REFXMLCDataSection::REFXMLCDataSection(const UniString& strData, REFXMLDocument* pDoc)
	: REFXMLLinkedNode(pDoc)
{
	m_strValue = strData;
}

REFXMLCDataSection::~REFXMLCDataSection()
{
}

// Gets the name of the node.
UniString REFXMLCDataSection::GetName() const
{
	return GetOwnerDocument()->m_strCDataSectionName;
}

// Gets the name of the node without the namespace prefix.
UniString REFXMLCDataSection::GetLocalName() const
{
	return GetOwnerDocument()->m_strCDataSectionName;
}

// Gets the type of the current node.
XMLNodeType REFXMLCDataSection::GetNodeType() const
{
	return XMLNodeType_CDATA;
}

REFXMLNode* REFXMLCDataSection::GetParentNode() const
{
	switch (m_pParentNode->GetNodeType())
	{
	case XMLNodeType_Document:
		return NULL;
	case XMLNodeType_Text:
	case XMLNodeType_CDATA:
	case XMLNodeType_Whitespace:
	case XMLNodeType_SignificantWhitespace:
		{
			REFXMLNode* pParent = m_pParentNode->m_pParentNode;
			while (pParent->IsText())
			{
				pParent = pParent->m_pParentNode;
			}
			return pParent;
		}
	default:
		return m_pParentNode;
	}
}

// Saves the node to the specified XmlWriter.
void REFXMLCDataSection::WriteTo(REFXMLWriter* w) const
{
	w->WriteCData(GetValue());
}

// Saves the node content to the specified XmlWriter.
void REFXMLCDataSection::WriteContentTo(REFXMLWriter* w) const
{
	// NOTE: intentionally do nothing
}

// Gets or sets the value of the node.
UniString REFXMLCDataSection::GetValue() const
{
	return m_strValue;
}
void REFXMLCDataSection::SetValue(const UniString& str)
{
	m_strValue = str;
}

// Gets or sets the concatenated values of the node and all its children.
UniString REFXMLCDataSection::GetInnerText() const
{
	return GetValue();
}
void REFXMLCDataSection::SetInnerText(const UniString& str)
{
	SetValue(str);
}

bool REFXMLCDataSection::IsText() const
{
	return true;
}

REFXMLNode* REFXMLCDataSection::GetPreviousText() const
{
	if (m_pParentNode->IsText())
		return m_pParentNode;
	return NULL;
}

}	//	namespace BFX
